package com.dwxt.sport.util;

import com.dwxt.common.base.BaseResult;
import com.dwxt.common.util.JsonUtils;
import com.dwxt.common.util.OkHttpUtils;
import com.dwxt.sport.entity.WifeDevice;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ShuWeiUtil {
    private static String baseUrl = "http://ws.papakaka.com";

    public static String getToken() {
        String token = RedisUtil.get("toke", String.class);
        if (StringUtils.isNotBlank(token)) {
            return token;
        } else {
            token = getTokenNow();
            RedisUtil.set("token", token, 60 * 60L);
            return token;
        }
    }

    public static String getTokenNow() {
        String token;
        String url = baseUrl + "/api/v1/token";
        Map<String, String> params = new HashMap<>();
        params.put("userName", "wansheng");
        params.put("password", "eM!jsMyy2jd");
        String result = OkHttpUtils.jsonPost(url, JsonUtils.objectToJson(params), null);
        token = JSONObject.fromObject(result).getString("data");
        return token;
    }

    public static JSONObject addBoxByMac(WifeDevice wifeDevice) {
        String url = baseUrl + "/api/v1/box/add-box-by-mac";
        Map params = new HashMap<>();
        params.put("lat", wifeDevice.getLat());
        params.put("lng", wifeDevice.getLng());
        params.put("mac", wifeDevice.getMacAddress());
        params.put("sceneId", wifeDevice.getId());
        params.put("sceneName", wifeDevice.getName());
        String token = getToken();
        Map headers = new HashMap();
        headers.put("token", token);
        String result = OkHttpUtils.jsonPost(url, JsonUtils.objectToJson(params), headers);
        return JSONObject.fromObject(result);
    }

    public static JSONObject deleteBox(String mac) {
        String url = baseUrl + "api/v1/box/delete-box?mac=" + mac;
        String token = getToken();
        Map headers = new HashMap();
        headers.put("token", token);
        String result = OkHttpUtils.synGet(url, headers);
        return JSONObject.fromObject(result);
    }

    public static JSONObject updateBox(WifeDevice wifeDevice) {
        String url = baseUrl + "/api/v1/box/update-box";
        Map params = new HashMap<>();
        params.put("isValid", true);
        params.put("lat", wifeDevice.getLat());
        params.put("lng", wifeDevice.getLng());
        params.put("mac", wifeDevice.getMacAddress());
        params.put("sceneId", wifeDevice.getId());
        params.put("sceneName", wifeDevice.getName());

        String token = getToken();
        Map headers = new HashMap();
        headers.put("token", token);
        JSONObject jsonObject = new JSONObject();
        String result = OkHttpUtils.jsonPost(url, jsonObject.fromObject(params).toString(), headers);
        return JSONObject.fromObject(result);
    }


    public static JSONObject timeDistribution(List<String> sceneIds) {
        String ids = JsonUtils.objectToJson(sceneIds).toLowerCase();
        String url = baseUrl + "/api/v1/time-distribution";
        String params = "{\"sceneIds\":" + ids + "}";
        System.out.println(params);
        String token = getToken();
        Map headers = new HashMap();
        headers.put("token", token);
        String result = OkHttpUtils.jsonPost(url, params, headers);
        System.out.println(token);
        return JSONObject.fromObject(result);
    }


    public static JSONObject crowdAttribute(List<String> sceneIds) {
        String ids = JsonUtils.objectToJson(sceneIds).toLowerCase();
        String url = baseUrl + "/api/v1/crowd-attribute";
        String params = "{\"sceneIds\":" + ids + "}";
        //System.out.println(params);
        String token = getToken();
        Map headers = new HashMap();
        headers.put("token", token);
        String result = OkHttpUtils.jsonPost(url, params, headers);
        System.out.println(token);
        return JSONObject.fromObject(result).getJSONObject("data");
    }
}
